//
//  DynamicSignalLabel.m
//  Portable Traffic Signal Assistor
//
//  Created by Ding on 9/6/12.
//  Copyright (c) 2012 TTU. All rights reserved.
//

#import "DynamicSignalLabel.h"

@implementation DynamicSignalLabel

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;


static const int TIMING_RING1_GRAPH_INITIAL_POSITION_X=5;
static const int TIMING_RING1_GRAPH_POSITION_Y=0;
static const int TIMING_RING1_GRAPH_HEIGHT=5;
static const int TIMING_RING2_GRAPH_INITIAL_POSITION_X=5;
static const int TIMING_RING2_GRAPH_POSITION_Y=6;
static const int TIMING_RING2_GRAPH_HEIGHT=5;
static const int SELF_FRAME_ORIGIN_Y=2;

@synthesize signal=_signal;
@synthesize signalNameLabel=_signalNameLabel;
@synthesize activePhaseLabel=_activePhaseLabel;
@synthesize timingRing1GraphPositionX=_timingRing1GraphPositionX;
@synthesize timingRing2GraphPositionX=_timingRing2GraphPositionX;
@synthesize offsetPhaseTime=_offsetPhaseTime;
@synthesize labelList=_labelList;
@synthesize subActivePhaseLabel=_subActivePhaseLabel;
@synthesize nbandLeftBeginPoints=_nbandLeftBeginPoints;
@synthesize nbandrightBeginPoints=_nbandrightBeginPoints;
@synthesize nbandLeftEndPoints=_nbandLeftEndPoints;
@synthesize nbandRightEndPoints=_nbandRightEndPoints;
@synthesize sbandLeftBeginPoints=_sbandLeftBeginPoints;
@synthesize sbandrightBeginPoints=_sbandrightBeginPoints;
@synthesize sbandLeftEndPoints=_sbandLeftEndPoints;
@synthesize sbandRightEndPoints=_sbandRightEndPoints;
@synthesize ebandLeftBeginPoints=_ebandLeftBeginPoints;
@synthesize ebandrightBeginPoints=_ebandrightBeginPoints;
@synthesize ebandLeftEndPoints=_ebandLeftEndPoints;
@synthesize ebandRightEndPoints=_ebandRightEndPoints;
@synthesize wbandLeftBeginPoints=_wbandLeftBeginPoints;
@synthesize wbandrightBeginPoints=_wbandrightBeginPoints;
@synthesize wbandLeftEndPoints=_wbandLeftEndPoints;
@synthesize wbandRightEndPoints=_wbandRightEndPoints;

@synthesize beforeLink=_beforeLink;
@synthesize afterLink=_afterLink;

@synthesize direction=_direction;
@synthesize labelsExtensionNumber=_labelsExtensionNumber;
@synthesize timingPattern=_timingPattern;
-(void) dealloc
{
    [_signal release];
    [_signalNameLabel release];
    [_activePhaseLabel release];
    [_labelList release];
    [_subActivePhaseLabel release];
    [_nbandLeftBeginPoints release];
    [_nbandrightBeginPoints release];
    [_nbandLeftEndPoints release];
    [_nbandRightEndPoints release];
    [_sbandLeftBeginPoints release];
    [_sbandrightBeginPoints release];
    [_sbandLeftEndPoints release];
    [_sbandRightEndPoints release];
    [_ebandLeftBeginPoints release];
    [_ebandrightBeginPoints release];
    [_ebandLeftEndPoints release];
    [_ebandRightEndPoints release];
    [_wbandLeftBeginPoints release];
    [_wbandrightBeginPoints release];
    [_wbandLeftEndPoints release];
    [_wbandRightEndPoints release];
    [_beforeLink release];
    [_afterLink release];
    [_timingPattern release];
    [super dealloc];
}
- (id)initWithSignal:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier signal:(Signal*)signal direction:(int)direction labelsExtensionNumber:(int)labelsExtensionNumber
{
    self.signal=signal;
    self.timingPattern=[XMLParser getTimingPattern:signal.timingPatternID agencyID:signal.agencyID subSystemID:signal.subSystemID];
    self.direction=direction;
    self.labelsExtensionNumber=labelsExtensionNumber;
    self = [super initWithFrame:CGRectMake(0, SELF_FRAME_ORIGIN_Y, 320*(self.labelsExtensionNumber+1), 21)];
    
    self.nbandLeftBeginPoints=[[[NSMutableArray alloc] init] autorelease];
    self.nbandLeftEndPoints=[[[NSMutableArray alloc] init] autorelease];
    self.nbandrightBeginPoints=[[[NSMutableArray alloc] init] autorelease];
    self.nbandRightEndPoints=[[[NSMutableArray alloc] init] autorelease];
    self.sbandLeftBeginPoints=[[[NSMutableArray alloc] init] autorelease];
    self.sbandLeftEndPoints=[[[NSMutableArray alloc] init] autorelease];
    self.sbandrightBeginPoints=[[[NSMutableArray alloc] init] autorelease];
    self.sbandRightEndPoints=[[[NSMutableArray alloc] init] autorelease];
    self.ebandLeftBeginPoints=[[[NSMutableArray alloc] init] autorelease];
    self.ebandLeftEndPoints=[[[NSMutableArray alloc] init] autorelease];
    self.ebandrightBeginPoints=[[[NSMutableArray alloc] init] autorelease];
    self.ebandRightEndPoints=[[[NSMutableArray alloc] init] autorelease];
    self.wbandLeftBeginPoints=[[[NSMutableArray alloc] init] autorelease];
    self.wbandLeftEndPoints=[[[NSMutableArray alloc] init] autorelease];
    self.wbandrightBeginPoints=[[[NSMutableArray alloc] init] autorelease];
    self.wbandRightEndPoints=[[[NSMutableArray alloc] init] autorelease];
    if (self) {
        // Initialization code
        
        self.signalNameLabel=[[[UILabel alloc] initWithFrame:CGRectMake(15, 2, 300, 10)] autorelease];
        self.signalNameLabel.backgroundColor=[UIColor whiteColor];
        self.signalNameLabel.numberOfLines=0;
        self.signalNameLabel.lineBreakMode=UILineBreakModeWordWrap;
        UIFont *cellFont = [ UIFont fontWithName: @"Arial" size: 10.0 ];
        self.signalNameLabel.minimumFontSize=5;
        //        self.signalNameLabel.font  = cellFont;
//        self.signalNameLabel.layer.cornerRadius=8;
        self.signalNameLabel.text=self.signal.signalName;
        self.signalNameLabel.font=cellFont;
        self.signalNameLabel.textAlignment=UITextAlignmentRight;
        
        self.activePhaseLabel=[[[UILabel alloc] initWithFrame:CGRectMake(0, 11, 320*(self.labelsExtensionNumber+1), 10)] autorelease];
        self.activePhaseLabel.backgroundColor=[UIColor clearColor];
        self.activePhaseLabel.numberOfLines=0;
        self.activePhaseLabel.lineBreakMode=UILineBreakModeWordWrap;
        self.activePhaseLabel.minimumFontSize=5;
        self.activePhaseLabel.font  = cellFont;
        self.activePhaseLabel.layer.cornerRadius=0;
        self.activePhaseLabel.layer.borderColor = [UIColor blackColor].CGColor;
        self.activePhaseLabel.layer.borderWidth = 1.0;
        self.labelList=[[[NSMutableArray alloc] init] autorelease];
        [self drawSignalGraph];
//        [self drawSignalGraph];
        [self addSubview:self.signalNameLabel];
        int signalNameNumber=self.activePhaseLabel.frame.size.width/320;
        for (int i=1; i<signalNameNumber; i++) {
            UILabel *tempSignalNameLabel=[[[UILabel alloc] initWithFrame:CGRectMake(15+320*i, 2, 300, 10)] autorelease];
            tempSignalNameLabel.backgroundColor=[UIColor whiteColor];
            tempSignalNameLabel.numberOfLines=0;
            tempSignalNameLabel.lineBreakMode=UILineBreakModeWordWrap;
            
            tempSignalNameLabel.minimumFontSize=5;
            //        self.signalNameLabel.font  = cellFont;
            //        self.signalNameLabel.layer.cornerRadius=8;
            tempSignalNameLabel.text=self.signal.signalName;
            tempSignalNameLabel.font=cellFont;
            tempSignalNameLabel.textAlignment=UITextAlignmentRight;
            [self addSubview:tempSignalNameLabel];
        }
        [self addSubview:self.activePhaseLabel];
        
        //set offset phase time
        {
            self.offsetPhaseTime=0;
            int i=0;
            for (i=0; i<self.signal.ring1.phaseList.count; i++) {
                SignalPhase *phase=[self.signal.ring1.phaseList objectAtIndex:i];
                if ([self.signal.offsetPhase isEqualToString:phase.phaseID]) {
                    if ([self.signal.offsetBorE isEqualToString:@"Green End"]) {
                        self.offsetPhaseTime=self.offsetPhaseTime+[phase.greenTime doubleValue];
                        
                    }
                    break;
                }
                self.offsetPhaseTime=self.offsetPhaseTime+[phase.greenTime doubleValue]+[phase.yellowTime doubleValue]+[phase.allRedTime doubleValue];
            }
            if (i>=self.signal.ring1.phaseList.count) {
                self.offsetPhaseTime=0;
                for (i=0; i<self.signal.ring2.phaseList.count; i++) {
                    SignalPhase *phase=[self.signal.ring2.phaseList objectAtIndex:i];
                    if ([self.signal.offsetPhase isEqualToString:phase.phaseID]) {
                        if ([self.signal.offsetBorE isEqualToString:@"Green End"]) {
                            self.offsetPhaseTime=self.offsetPhaseTime+[phase.greenTime doubleValue];
                            
                        }
                        break;
                    }
                    self.offsetPhaseTime=self.offsetPhaseTime+[phase.greenTime doubleValue]+[phase.yellowTime doubleValue]+[phase.allRedTime doubleValue];
                }
                if (i>=self.signal.ring2.phaseList.count) {
                    self.offsetPhaseTime=0;
                }
            }
        }
    

    }
    
    return self;
    
}

//- (void)setSelected:(BOOL)selected animated:(BOOL)animated
//{
//    [super setSelected:selected animated:animated];
//    
//    // Configure the view for the selected state
//}

- (void)drawSignalGraph
{
//    [self.labelList removeAllObjects];
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, 320*(self.labelsExtensionNumber+1), self.frame.size.height);
    self.subActivePhaseLabel=[[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320*(self.labelsExtensionNumber+1), 10)] autorelease];
    self.timingRing1GraphPositionX=TIMING_RING1_GRAPH_INITIAL_POSITION_X;
    self.timingRing2GraphPositionX=TIMING_RING2_GRAPH_INITIAL_POSITION_X;
    int timingGraphPositionY=TIMING_RING1_GRAPH_POSITION_Y;
    int timingGraphHeight=TIMING_RING1_GRAPH_HEIGHT;
    
    NSArray *ring1PhaseList=self.signal.ring1.phaseList;
    NSArray *ring2PhaseList=self.signal.ring2.phaseList;
    
    int ring1BarrierTime=0;
    for (int i=0; i<ring1PhaseList.count; i++) {
        SignalPhase *phase=[ring1PhaseList objectAtIndex:i];
        if ([phase.barrier intValue]==1) {
            int greenPhaseLength=round(320*[phase.greenTime doubleValue]/[self.timingPattern.cycleLength intValue]);
            int yellowPhaseLength=round(320*[phase.yellowTime doubleValue]/[self.timingPattern.cycleLength intValue]);
            int allRedPhaseLength=round(320*[phase.allRedTime doubleValue]/[self.timingPattern.cycleLength intValue]);
            ring1BarrierTime=ring1BarrierTime+greenPhaseLength+yellowPhaseLength+allRedPhaseLength;
        }
    }
    ring1BarrierTime=ring1BarrierTime+self.timingRing1GraphPositionX;
    int ring2BarrierTime=0;
    for (int i=0; i<ring2PhaseList.count; i++) {
        SignalPhase *phase=[ring2PhaseList objectAtIndex:i];
        if ([phase.barrier intValue]==1) {
            int greenPhaseLength=round(320*[phase.greenTime doubleValue]/[self.timingPattern.cycleLength intValue]);
            int yellowPhaseLength=round(320*[phase.yellowTime doubleValue]/[self.timingPattern.cycleLength intValue]);
            int allRedPhaseLength=round(320*[phase.allRedTime doubleValue]/[self.timingPattern.cycleLength intValue]);
            ring2BarrierTime=ring2BarrierTime+greenPhaseLength+yellowPhaseLength+allRedPhaseLength;
        }
    }
    ring2BarrierTime=ring2BarrierTime+self.timingRing2GraphPositionX;
    double barrierTime=0;
    if (ring1BarrierTime>=ring2BarrierTime) {
        barrierTime=ring1BarrierTime;
    }else{
        barrierTime=ring2BarrierTime;
    }
    int barrierPositionX=barrierTime;
    
    for (int i=0; i<ring1PhaseList.count; i++) {
        SignalPhase *phase=[ring1PhaseList objectAtIndex:i];
//        SignalPhase *phase=[signal.ring1.phaseList objectAtIndex:i];
        //        NSString *phaseMovement=phase.phaseMovement;
        
        if ([phase.barrier intValue]==2 && self.timingRing1GraphPositionX<barrierPositionX) {
            self.timingRing1GraphPositionX=barrierPositionX;
        }
        
        
        int greenPhaseLength=round(320*[phase.greenTime doubleValue]/[self.timingPattern.cycleLength intValue]);
        int yellowPhaseLength=round(320*[phase.yellowTime doubleValue]/[self.timingPattern.cycleLength intValue]);
        int allRedPhaseLength=round(320*[phase.allRedTime doubleValue]/[self.timingPattern.cycleLength intValue]);
        UILabel *greenLabel = [[[UILabel alloc] initWithFrame:CGRectMake(self.timingRing1GraphPositionX,timingGraphPositionY,greenPhaseLength,timingGraphHeight)] autorelease];
        greenLabel.tag=100+i;
//        greenLabel.text =[phase.phaseID stringByAppendingString:phase.phaseMovement];
        greenLabel.font = [UIFont boldSystemFontOfSize:8];
        greenLabel.textAlignment = UITextAlignmentCenter;
        greenLabel.backgroundColor = [UIColor redColor];
        greenLabel.textColor=[UIColor blackColor];
        
        

        //        [self addLabelResponder:greenLabel];
        
        
        self.timingRing1GraphPositionX=self.timingRing1GraphPositionX+greenPhaseLength;
        
        UILabel *yellowLabel = [[[UILabel alloc] initWithFrame:CGRectMake(self.timingRing1GraphPositionX,timingGraphPositionY,yellowPhaseLength,timingGraphHeight)] autorelease];
        yellowLabel.backgroundColor = [UIColor redColor];
        
       
        self.timingRing1GraphPositionX=self.timingRing1GraphPositionX+yellowPhaseLength;
        
        UILabel *allRedLabel = [[[UILabel alloc] initWithFrame:CGRectMake(self.timingRing1GraphPositionX,timingGraphPositionY,allRedPhaseLength,timingGraphHeight)] autorelease];
        allRedLabel.backgroundColor = [UIColor redColor];
        
        {
            NSString* phaseMovement=phase.phaseMovement;
            NSMutableArray* movementList=[[[NSMutableArray alloc] init] autorelease];
            NSRange commaRange=[phaseMovement rangeOfString:@","];
            while (commaRange.location!=NSNotFound) {
                NSString *movement=[phaseMovement substringToIndex:commaRange.location];
                [movementList addObject:movement];
                phaseMovement=[phaseMovement substringFromIndex:(commaRange.location+1)];
                commaRange=[phaseMovement rangeOfString:@","];
            }
            if (phaseMovement.length>0) {
                [movementList addObject:phaseMovement];
            }
            switch (self.direction) {
                case NORTHBOUND:
                case SOUTHBOUND:
                {
                    for (NSString *movement in movementList) {
                        NSRange isRange=[movement rangeOfString:@"NBT"];
                        if ((isRange.location!=NSNotFound||[movement isEqualToString:@"NBSB"] ||[movement isEqualToString:@"NBTL"])) {
                            if (yellowLabel.backgroundColor!=[UIColor yellowColor]) {
                                greenLabel.backgroundColor=[UIColor blueColor];
                                greenLabel.textColor=[UIColor blackColor];
                                yellowLabel.backgroundColor=[UIColor yellowColor];
                            }
                            
                            InterfacePoint *greenTopLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                            NSLog(@"\n Parameters: %f,%f,%f,%f,%f,%f,%f",greenTopLeftPoint.X,greenTopLeftPoint.Y,greenLabel.frame.origin.x,self.frame.origin.x,self.subActivePhaseLabel.frame.origin.x,SELF_FRAME_ORIGIN_Y,self.signalNameLabel.frame.size.height);
                            InterfacePoint *yellowTopRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                            [self.nbandLeftEndPoints addObject:greenTopLeftPoint];
                            [self.nbandRightEndPoints addObject:yellowTopRightPoint];
                            InterfacePoint *greenBottomLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                            InterfacePoint *yellowBottomRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                            
                            [self.nbandLeftBeginPoints addObject:greenBottomLeftPoint];
                            
                            [self.nbandrightBeginPoints addObject:yellowBottomRightPoint];
                            NSLog(@"%@\n",movement);
                        }
                        //[movement isEqualToString:@"NBT"]||[movement isEqualToString:@"NBSB"] ||[movement isEqualToString:@"SBTL"]||
                        isRange=[movement rangeOfString:@"NBL"];
                        if ((isRange.location!=NSNotFound||[movement isEqualToString:@"NBTL"])) {
                            greenLabel.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"BlueUpLeft.png"]];
                            //                    greenLabel.backgroundColor=[UIColor colorWithRed:176 green:226 blue:255 alpha:1.0];
                            yellowLabel.backgroundColor=[UIColor yellowColor];
                            NSLog(@"%@\n",movement);
                        }
                    }
                }
                
                {
                    for (NSString *movement in movementList) {
                        NSRange isRange=[movement rangeOfString:@"SBT"];
                        if ((isRange.location!=NSNotFound||[movement isEqualToString:@"NBSB"] )) {
                            if (yellowLabel.backgroundColor!=[UIColor yellowColor]) {
                                greenLabel.backgroundColor=[UIColor greenColor];
                                greenLabel.textColor=[UIColor blackColor];
                                yellowLabel.backgroundColor=[UIColor yellowColor];
                            }
                            InterfacePoint *greenTopLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                            InterfacePoint *yellowTopRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                            [self.sbandLeftEndPoints addObject:greenTopLeftPoint];
                            [self.sbandRightEndPoints addObject:yellowTopRightPoint];
                            InterfacePoint *greenBottomLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                            InterfacePoint *yellowBottomRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                            
                            [self.sbandLeftBeginPoints addObject:greenBottomLeftPoint];
                            
                            [self.sbandrightBeginPoints addObject:yellowBottomRightPoint];
                            NSLog(@"%@\n",movement);
                        }
//                        [movement isEqualToString:@"NBT"]||[movement isEqualToString:@"NBSB"] ||[movement isEqualToString:@"NBTL"]||
                        isRange=[movement rangeOfString:@"SBL"];
                        if ((isRange.location!=NSNotFound||[movement isEqualToString:@"SBTL"])) {
                            greenLabel.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"DownLeft.png"]];
                            //                    greenLabel.backgroundColor=[UIColor colorWithRed:176 green:226 blue:255 alpha:1.0];
                            yellowLabel.backgroundColor=[UIColor yellowColor];
                            NSLog(@"%@\n",movement);
                        }
                    }
                }
                    break;
                case EASTBOUND:
                case WESTBOUND:
                {
                    for (NSString *movement in movementList) {
                        NSRange isRange=[movement rangeOfString:@"EBT"];
                        if ((isRange.location!=NSNotFound )) {
                            if (yellowLabel.backgroundColor!=[UIColor yellowColor]) {
                                greenLabel.backgroundColor=[UIColor greenColor];
                                greenLabel.textColor=[UIColor blackColor];
                                yellowLabel.backgroundColor=[UIColor yellowColor];
                            }
                            InterfacePoint *greenTopLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                            InterfacePoint *yellowTopRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                            [self.ebandLeftEndPoints addObject:greenTopLeftPoint];
                            [self.ebandRightEndPoints addObject:yellowTopRightPoint];
                            InterfacePoint *greenBottomLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                            InterfacePoint *yellowBottomRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                            
                            [self.ebandLeftBeginPoints addObject:greenBottomLeftPoint];
                            
                            [self.ebandrightBeginPoints addObject:yellowBottomRightPoint];
                            NSLog(@"%@\n",movement);
                        }
                        //[movement isEqualToString:@"WBT"] ||[movement isEqualToString:@"WBTL"]||
                        isRange=[movement rangeOfString:@"EBL"];
                        if ((isRange.location!=NSNotFound||[movement isEqualToString:@"EBTL"])) {
                            greenLabel.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"DownLeft.png"]];
                            //                    greenLabel.backgroundColor=[UIColor colorWithRed:176 green:226 blue:255 alpha:1.0];
                            yellowLabel.backgroundColor=[UIColor yellowColor];
                            NSLog(@"%@\n",movement);
                        }
                    }
                }
                
                {
                    for (NSString *movement in movementList) {
                         NSRange isRange=[movement rangeOfString:@"WBT"];
                        if ((isRange.location!=NSNotFound )) {
                            if (yellowLabel.backgroundColor!=[UIColor yellowColor]) {
                                greenLabel.backgroundColor=[UIColor greenColor];
                                greenLabel.textColor=[UIColor blackColor];
                                yellowLabel.backgroundColor=[UIColor yellowColor];
                            }
                            InterfacePoint *greenTopLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                            InterfacePoint *yellowTopRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                            [self.wbandLeftEndPoints addObject:greenTopLeftPoint];
                            [self.wbandRightEndPoints addObject:yellowTopRightPoint];
                            InterfacePoint *greenBottomLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                            InterfacePoint *yellowBottomRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                            [self.wbandLeftBeginPoints addObject:greenBottomLeftPoint];
                            
                            
                            [self.wbandrightBeginPoints addObject:yellowBottomRightPoint];
                            NSLog(@"%@\n",movement);
                        }
                        //[movement isEqualToString:@"EBT"] ||[movement isEqualToString:@"EBTL"]||
                        isRange=[movement rangeOfString:@"WBL"];
                        if ((isRange.location!=NSNotFound||[movement isEqualToString:@"WBTL"])) {
                            greenLabel.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"BlueUpLeft.png"]];
                            //                    greenLabel.backgroundColor=[UIColor colorWithRed:176 green:226 blue:255 alpha:1.0];
                            yellowLabel.backgroundColor=[UIColor yellowColor];
                            NSLog(@"%@\n",movement);
                        }
                    }
                }
                    break;
                    
                    
                default:
                    break;
            }
            
        }
        [self.labelList addObject:greenLabel];
        [self.labelList addObject:yellowLabel];
        [self.labelList addObject:allRedLabel];
        UILabel *offsetLabel = [[[UILabel alloc] initWithFrame:CGRectMake(allRedLabel.frame.origin.x+allRedLabel.frame.size.width,0,1,self.subActivePhaseLabel.frame.size.height)] autorelease];
        offsetLabel.backgroundColor = [UIColor blueColor];
        [self.subActivePhaseLabel addSubview:greenLabel];
        [self.subActivePhaseLabel addSubview:yellowLabel];
        [self.subActivePhaseLabel addSubview:allRedLabel];
        [self.subActivePhaseLabel addSubview:offsetLabel];
        self.timingRing1GraphPositionX=self.timingRing1GraphPositionX+allRedPhaseLength;
    }
    {
        UILabel *barrierLabel = [[[UILabel alloc] initWithFrame:CGRectMake(barrierPositionX,timingGraphPositionY,5,timingGraphHeight)] autorelease];
        barrierLabel.backgroundColor = [UIColor blackColor];
//        [self.labelList addObject:barrierLabel];
        [self.subActivePhaseLabel addSubview:barrierLabel];
    }
    
    for (int j=0; j<self.labelsExtensionNumber; j++) {
        self.timingRing1GraphPositionX=TIMING_RING1_GRAPH_INITIAL_POSITION_X;
        for (int i=0; i<ring1PhaseList.count; i++) {
            SignalPhase *phase=[ring1PhaseList objectAtIndex:i];
            if ([phase.barrier intValue]==2 && self.timingRing1GraphPositionX<barrierPositionX) {
                self.timingRing1GraphPositionX=barrierPositionX;
            }
            int greenPhaseLength=round(320*[phase.greenTime doubleValue]/[self.timingPattern.cycleLength intValue]);
            int yellowPhaseLength=round(320*[phase.yellowTime doubleValue]/[self.timingPattern.cycleLength intValue]);
            int allRedPhaseLength=round(320*[phase.allRedTime doubleValue]/[self.timingPattern.cycleLength intValue]);
            UILabel *greenLabel = [[[UILabel alloc] initWithFrame:CGRectMake(self.timingRing1GraphPositionX+round((j+1)*320*[self.signal.cycleLength doubleValue]/[self.timingPattern.cycleLength doubleValue]),timingGraphPositionY,greenPhaseLength,timingGraphHeight)] autorelease];
            greenLabel.tag=100+i;
            //        greenLabel.text =[phase.phaseID stringByAppendingString:phase.phaseMovement];
            greenLabel.font = [UIFont boldSystemFontOfSize:8];
            greenLabel.textAlignment = UITextAlignmentCenter;
            greenLabel.backgroundColor = [UIColor redColor];
            greenLabel.textColor=[UIColor blackColor];
            //        [self addLabelResponder:greenLabel];
            //        [self.labelList addObject:greenLabel];
            //        [self.subActivePhaseLabel addSubview:greenLabel];
            self.timingRing1GraphPositionX=self.timingRing1GraphPositionX+greenPhaseLength;
            
            UILabel *yellowLabel = [[[UILabel alloc] initWithFrame:CGRectMake(self.timingRing1GraphPositionX+round((j+1)*320*[self.signal.cycleLength doubleValue]/[self.timingPattern.cycleLength doubleValue]),timingGraphPositionY,yellowPhaseLength,timingGraphHeight)] autorelease];
            yellowLabel.backgroundColor = [UIColor redColor];
            
            //        [self.labelList addObject:yellowLabel];
            //        [self.subActivePhaseLabel addSubview:yellowLabel];
            self.timingRing1GraphPositionX=self.timingRing1GraphPositionX+yellowPhaseLength;
            
            UILabel *allRedLabel = [[[UILabel alloc] initWithFrame:CGRectMake(self.timingRing1GraphPositionX+round((j+1)*320*[self.signal.cycleLength doubleValue]/[self.timingPattern.cycleLength doubleValue]),timingGraphPositionY,allRedPhaseLength,timingGraphHeight)] autorelease];
            allRedLabel.backgroundColor = [UIColor redColor];
            {
                NSString* phaseMovement=phase.phaseMovement;
                NSMutableArray* movementList=[[[NSMutableArray alloc] init] autorelease];
                NSRange commaRange=[phaseMovement rangeOfString:@","];
                while (commaRange.location!=NSNotFound) {
                    NSString *movement=[phaseMovement substringToIndex:commaRange.location];
                    [movementList addObject:movement];
                    phaseMovement=[phaseMovement substringFromIndex:(commaRange.location+1)];
                    commaRange=[phaseMovement rangeOfString:@","];
                }
                if (phaseMovement.length>0) {
                    [movementList addObject:phaseMovement];
                }
                
                switch (self.direction) {
                    case NORTHBOUND:
                    case SOUTHBOUND:
                    {
                        for (NSString *movement in movementList) {
                            NSRange isRange=[movement rangeOfString:@"NBT"];
                            if ((isRange.location!=NSNotFound||[movement isEqualToString:@"NBSB"] ||[movement isEqualToString:@"NBTL"])) {
                                if (yellowLabel.backgroundColor!=[UIColor yellowColor]) {
                                    greenLabel.backgroundColor=[UIColor blueColor];
                                    greenLabel.textColor=[UIColor blackColor];
                                    yellowLabel.backgroundColor=[UIColor yellowColor];
                                }
                                InterfacePoint *greenTopLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                                InterfacePoint *yellowTopRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                                [self.nbandLeftEndPoints addObject:greenTopLeftPoint];
                                [self.nbandRightEndPoints addObject:yellowTopRightPoint];
                                InterfacePoint *greenBottomLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                                InterfacePoint *yellowBottomRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                                [self.nbandLeftBeginPoints addObject:greenBottomLeftPoint];
                                
                                [self.nbandrightBeginPoints addObject:yellowBottomRightPoint];
                                NSLog(@"%@\n",movement);
                            }
                            //[movement isEqualToString:@"NBT"]||[movement isEqualToString:@"NBSB"] ||[movement isEqualToString:@"SBTL"]||
                            isRange=[movement rangeOfString:@"NBL"];
                            if ((isRange.location!=NSNotFound||[movement isEqualToString:@"NBTL"])) {
                                greenLabel.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"BlueUpLeft.png"]];
                                //                    greenLabel.backgroundColor=[UIColor colorWithRed:176 green:226 blue:255 alpha:1.0];
                                yellowLabel.backgroundColor=[UIColor yellowColor];
                                NSLog(@"%@\n",movement);
                            }
                        }
                    }
                        
                    {
                        for (NSString *movement in movementList) {
                            NSRange isRange=[movement rangeOfString:@"SBT"];
                            if ((isRange.location!=NSNotFound||[movement isEqualToString:@"NBSB"] ||[movement isEqualToString:@"SBTL"])) {
                                if (yellowLabel.backgroundColor!=[UIColor yellowColor]) {
                                    greenLabel.backgroundColor=[UIColor greenColor];
                                    greenLabel.textColor=[UIColor blackColor];
                                    yellowLabel.backgroundColor=[UIColor yellowColor];
                                }
                                InterfacePoint *greenTopLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                                InterfacePoint *yellowTopRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                                [self.sbandLeftEndPoints addObject:greenTopLeftPoint];
                                [self.sbandRightEndPoints addObject:yellowTopRightPoint];
                                InterfacePoint *greenBottomLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                                InterfacePoint *yellowBottomRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                                [self.sbandLeftBeginPoints addObject:greenBottomLeftPoint];
                                
                                [self.sbandrightBeginPoints addObject:yellowBottomRightPoint];
                                NSLog(@"%@\n",movement);
                            }
                            //                        [movement isEqualToString:@"NBT"]||[movement isEqualToString:@"NBSB"] ||[movement isEqualToString:@"NBTL"]||
                            isRange=[movement rangeOfString:@"SBL"];
                            if ((isRange.location!=NSNotFound||[movement isEqualToString:@"SBTL"])) {
                                greenLabel.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"DownLeft.png"]];
                                //                    greenLabel.backgroundColor=[UIColor colorWithRed:176 green:226 blue:255 alpha:1.0];
                                yellowLabel.backgroundColor=[UIColor yellowColor];
                                NSLog(@"%@\n",movement);
                            }
                        }
                    }
                        break;
                    case EASTBOUND:
                    case WESTBOUND:
                    {
                        for (NSString *movement in movementList) {
                            NSRange isRange=[movement rangeOfString:@"EBT"];
                            if ((isRange.location!=NSNotFound ||[movement isEqualToString:@"EBTL"])) {
                                if (yellowLabel.backgroundColor!=[UIColor yellowColor]) {
                                    greenLabel.backgroundColor=[UIColor greenColor];
                                    greenLabel.textColor=[UIColor blackColor];
                                    yellowLabel.backgroundColor=[UIColor yellowColor];
                                }
                                InterfacePoint *greenTopLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                                InterfacePoint *yellowTopRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                                [self.ebandLeftEndPoints addObject:greenTopLeftPoint];
                                [self.ebandRightEndPoints addObject:yellowTopRightPoint];
                                InterfacePoint *greenBottomLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                                InterfacePoint *yellowBottomRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                                [self.ebandLeftBeginPoints addObject:greenBottomLeftPoint];
                                
                                [self.ebandrightBeginPoints addObject:yellowBottomRightPoint];
                                NSLog(@"%@\n",movement);
                            }
                            //                        [movement isEqualToString:@"WBT"] ||[movement isEqualToString:@"WBTL"]||
                            isRange=[movement rangeOfString:@"EBL"];
                            if ((isRange.location!=NSNotFound||[movement isEqualToString:@"EBTL"])) {
                                greenLabel.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"DownLeft.png"]];
                                //                    greenLabel.backgroundColor=[UIColor colorWithRed:176 green:226 blue:255 alpha:1.0];
                                yellowLabel.backgroundColor=[UIColor yellowColor];
                                NSLog(@"%@\n",movement);
                            }
                        }
                    }
                        
                    {
                        for (NSString *movement in movementList) {
                            NSRange isRange=[movement rangeOfString:@"WBT"];
                            if ((isRange.location!=NSNotFound ||[movement isEqualToString:@"WBTL"])) {
                                if (yellowLabel.backgroundColor!=[UIColor yellowColor]) {
                                    greenLabel.backgroundColor=[UIColor blueColor];
                                    greenLabel.textColor=[UIColor blackColor];
                                    yellowLabel.backgroundColor=[UIColor yellowColor];
                                }
                                InterfacePoint *greenTopLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                                InterfacePoint *yellowTopRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                                [self.wbandLeftEndPoints addObject:greenTopLeftPoint];
                                [self.wbandRightEndPoints addObject:yellowTopRightPoint];
                                InterfacePoint *greenBottomLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                                InterfacePoint *yellowBottomRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                                [self.wbandLeftBeginPoints addObject:greenBottomLeftPoint];
                                
                                [self.wbandrightBeginPoints addObject:yellowBottomRightPoint];
                                NSLog(@"%@\n",movement);
                            }
                            //                        [movement isEqualToString:@"EBT"] ||[movement isEqualToString:@"EBTL"]||
                            isRange=[movement rangeOfString:@"WBL"];
                            if ((isRange.location!=NSNotFound||[movement isEqualToString:@"WBTL"])) {
                                greenLabel.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"BlueUpLeft.png"]];
                                //                    greenLabel.backgroundColor=[UIColor colorWithRed:176 green:226 blue:255 alpha:1.0];
                                yellowLabel.backgroundColor=[UIColor yellowColor];
                                NSLog(@"%@\n",movement);
                            }
                        }
                    }
                        break;
                        
                        
                    default:
                        break;
                }
            }
            [self.labelList addObject:greenLabel];
            [self.labelList addObject:yellowLabel];
            [self.labelList addObject:allRedLabel];
            //        [self.labelList addObject:allRedLabel];
            UILabel *offsetLabel = [[[UILabel alloc] initWithFrame:CGRectMake(allRedLabel.frame.origin.x+allRedLabel.frame.size.width,0,1,self.subActivePhaseLabel.frame.size.height)] autorelease];
            offsetLabel.backgroundColor = [UIColor blueColor];
            [self.subActivePhaseLabel addSubview:greenLabel];
            [self.subActivePhaseLabel addSubview:yellowLabel];
            [self.subActivePhaseLabel addSubview:allRedLabel];
            [self.subActivePhaseLabel addSubview:offsetLabel];
            self.timingRing1GraphPositionX=self.timingRing1GraphPositionX+allRedPhaseLength;
        }
        {
            UILabel *barrierLabel = [[[UILabel alloc] initWithFrame:CGRectMake(barrierPositionX+round((j+1)*320*[self.signal.cycleLength doubleValue]/[self.timingPattern.cycleLength doubleValue]),timingGraphPositionY,5,timingGraphHeight)] autorelease];
            barrierLabel.backgroundColor = [UIColor blackColor];
            //        [self.labelList addObject:barrierLabel];
            [self.subActivePhaseLabel addSubview:barrierLabel];
        }
    }
    
    
    
    timingGraphPositionY=TIMING_RING2_GRAPH_POSITION_Y;
    timingGraphHeight=TIMING_RING2_GRAPH_HEIGHT;
    for (int i=0; i<ring2PhaseList.count; i++) {
        SignalPhase *phase=[ring2PhaseList objectAtIndex:i];
        if ([phase.barrier intValue]==2 && self.timingRing2GraphPositionX<barrierPositionX) {
            self.timingRing2GraphPositionX=barrierPositionX;
        }
        int greenPhaseLength=round(320*[phase.greenTime doubleValue]/[self.timingPattern.cycleLength intValue]);
        int yellowPhaseLength=round(320*[phase.yellowTime doubleValue]/[self.timingPattern.cycleLength intValue]);
        int allRedPhaseLength=round(320*[phase.allRedTime doubleValue]/[self.timingPattern.cycleLength intValue]);
        UILabel *greenLabel = [[[UILabel alloc] initWithFrame:CGRectMake(self.timingRing2GraphPositionX,timingGraphPositionY,greenPhaseLength,timingGraphHeight)] autorelease];
        greenLabel.tag=200+i;
//        greenLabel.text =[phase.phaseID stringByAppendingString:phase.phaseMovement];
        greenLabel.font = [UIFont boldSystemFontOfSize:8];
        greenLabel.textAlignment = UITextAlignmentCenter;
        greenLabel.backgroundColor = [UIColor redColor];
        greenLabel.textColor=[UIColor blackColor];
        //        [self addLabelResponder:greenLabel];
//        [self.labelList addObject:greenLabel];
//        [self.subActivePhaseLabel addSubview:greenLabel];
        
        //        [self.labelList addObject:greenLabel];
        //        UILabel * tempLabel=[self.labelList objectAtIndex:self.labelList.count-1];
        //        [[self.labelList objectAtIndex:self.labelList.count-1] removeFromSuperview];
        
        self.timingRing2GraphPositionX=self.timingRing2GraphPositionX+greenPhaseLength;
        
        UILabel *yellowLabel = [[[UILabel alloc] initWithFrame:CGRectMake(self.timingRing2GraphPositionX,timingGraphPositionY,yellowPhaseLength,timingGraphHeight)] autorelease];
        yellowLabel.backgroundColor = [UIColor redColor];
        
//        [self.labelList addObject:yellowLabel];
//        [self.subActivePhaseLabel addSubview:yellowLabel];
        self.timingRing2GraphPositionX=self.timingRing2GraphPositionX+yellowPhaseLength;
        
        UILabel *allRedLabel = [[[UILabel alloc] initWithFrame:CGRectMake(self.timingRing2GraphPositionX,timingGraphPositionY,allRedPhaseLength,timingGraphHeight)] autorelease];
        allRedLabel.backgroundColor = [UIColor redColor];
        {
            NSString* phaseMovement=phase.phaseMovement;
            NSMutableArray* movementList=[[[NSMutableArray alloc] init] autorelease];
            NSRange commaRange=[phaseMovement rangeOfString:@","];
            while (commaRange.location!=NSNotFound) {
                NSString *movement=[phaseMovement substringToIndex:commaRange.location];
                [movementList addObject:movement];
                phaseMovement=[phaseMovement substringFromIndex:(commaRange.location+1)];
                commaRange=[phaseMovement rangeOfString:@","];
            }
            if (phaseMovement.length>0) {
                [movementList addObject:phaseMovement];
            }
            
            switch (self.direction) {
                case NORTHBOUND:
                case SOUTHBOUND:
                {
                    for (NSString *movement in movementList) {
                        NSRange isRange=[movement rangeOfString:@"NBT"];
                        if ((isRange.location!=NSNotFound||[movement isEqualToString:@"NBSB"] ||[movement isEqualToString:@"NBTL"])) {
                            if (yellowLabel.backgroundColor!=[UIColor yellowColor]) {
                                greenLabel.backgroundColor=[UIColor blueColor];
                                greenLabel.textColor=[UIColor blackColor];
                                yellowLabel.backgroundColor=[UIColor yellowColor];
                            }
                            InterfacePoint *greenTopLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                            InterfacePoint *yellowTopRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                            [self.nbandLeftEndPoints addObject:greenTopLeftPoint];
                            [self.nbandRightEndPoints addObject:yellowTopRightPoint];
                            InterfacePoint *greenBottomLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                            InterfacePoint *yellowBottomRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                            [self.nbandLeftBeginPoints addObject:greenBottomLeftPoint];
                            
                            [self.nbandrightBeginPoints addObject:yellowBottomRightPoint];
                            NSLog(@"%@\n",movement);
                        }
//                        [movement isEqualToString:@"NBT"]||[movement isEqualToString:@"NBSB"] ||[movement isEqualToString:@"SBTL"]||
                        isRange=[movement rangeOfString:@"NBL"];
                        if ((isRange.location!=NSNotFound||[movement isEqualToString:@"NBTL"])) {
                            greenLabel.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"BlueUpLeft.png"]];
                            //                    greenLabel.backgroundColor=[UIColor colorWithRed:176 green:226 blue:255 alpha:1.0];
                            yellowLabel.backgroundColor=[UIColor yellowColor];
                            NSLog(@"%@\n",movement);
                        }
                    }
                }
                    
                {
                    for (NSString *movement in movementList) {
                        NSRange isRange=[movement rangeOfString:@"SBT"];
                        if ((isRange.location!=NSNotFound||[movement isEqualToString:@"NBSB"] ||[movement isEqualToString:@"SBTL"])) {
                            if (yellowLabel.backgroundColor!=[UIColor yellowColor]) {
                                greenLabel.backgroundColor=[UIColor greenColor];
                                greenLabel.textColor=[UIColor blackColor];
                                yellowLabel.backgroundColor=[UIColor yellowColor];
                            }
                            InterfacePoint *greenTopLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                            InterfacePoint *yellowTopRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                            [self.sbandLeftEndPoints addObject:greenTopLeftPoint];
                            [self.sbandRightEndPoints addObject:yellowTopRightPoint];
                            InterfacePoint *greenBottomLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                            InterfacePoint *yellowBottomRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                            [self.sbandLeftBeginPoints addObject:greenBottomLeftPoint];
                            
                            [self.sbandrightBeginPoints addObject:yellowBottomRightPoint];
                            NSLog(@"%@\n",movement);
                        }
//                        [movement isEqualToString:@"NBT"]||[movement isEqualToString:@"NBSB"] ||[movement isEqualToString:@"NBTL"]||
                        isRange=[movement rangeOfString:@"SBL"];
                        if ((isRange.location!=NSNotFound||[movement isEqualToString:@"SBTL"])) {
                            greenLabel.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"DownLeft.png"]];
                            //                    greenLabel.backgroundColor=[UIColor colorWithRed:176 green:226 blue:255 alpha:1.0];
                            yellowLabel.backgroundColor=[UIColor yellowColor];
                            NSLog(@"%@\n",movement);
                        }
                    }
                }
                    break;
                case EASTBOUND:
                case WESTBOUND:
                {
                    for (NSString *movement in movementList) {
                         NSRange isRange=[movement rangeOfString:@"EBT"];
                        if ((isRange.location!=NSNotFound ||[movement isEqualToString:@"EBTL"])) {
                            if (yellowLabel.backgroundColor!=[UIColor yellowColor]) {
                                greenLabel.backgroundColor=[UIColor greenColor];
                                greenLabel.textColor=[UIColor blackColor];
                                yellowLabel.backgroundColor=[UIColor yellowColor];
                            }
                            InterfacePoint *greenTopLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                            InterfacePoint *yellowTopRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                            [self.ebandLeftEndPoints addObject:greenTopLeftPoint];
                            [self.ebandRightEndPoints addObject:yellowTopRightPoint];
                            InterfacePoint *greenBottomLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                            InterfacePoint *yellowBottomRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                            [self.ebandLeftBeginPoints addObject:greenBottomLeftPoint];
                            
                            [self.ebandrightBeginPoints addObject:yellowBottomRightPoint];
                            NSLog(@"%@\n",movement);
                        }
//                        [movement isEqualToString:@"WBT"] ||[movement isEqualToString:@"WBTL"]||
                        isRange=[movement rangeOfString:@"EBL"];
                        if ((isRange.location!=NSNotFound||[movement isEqualToString:@"EBTL"])) {
                            greenLabel.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"DownLeft.png"]];
                            //                    greenLabel.backgroundColor=[UIColor colorWithRed:176 green:226 blue:255 alpha:1.0];
                            yellowLabel.backgroundColor=[UIColor yellowColor];
                            NSLog(@"%@\n",movement);
                        }
                    }
                }
                    
                {
                    for (NSString *movement in movementList) {
                         NSRange isRange=[movement rangeOfString:@"WBT"];
                        if ((isRange.location!=NSNotFound ||[movement isEqualToString:@"WBTL"])) {
                            if (yellowLabel.backgroundColor!=[UIColor yellowColor]) {
                                greenLabel.backgroundColor=[UIColor blueColor];
                                greenLabel.textColor=[UIColor blackColor];
                                yellowLabel.backgroundColor=[UIColor yellowColor];
                            }
                            InterfacePoint *greenTopLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                            InterfacePoint *yellowTopRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                            [self.wbandLeftEndPoints addObject:greenTopLeftPoint];
                            [self.wbandRightEndPoints addObject:yellowTopRightPoint];
                            InterfacePoint *greenBottomLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                            InterfacePoint *yellowBottomRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                            [self.wbandLeftBeginPoints addObject:greenBottomLeftPoint];
                            
                            [self.wbandrightBeginPoints addObject:yellowBottomRightPoint];
                            NSLog(@"%@\n",movement);
                        }
//                        [movement isEqualToString:@"EBT"] ||[movement isEqualToString:@"EBTL"]||
                        isRange=[movement rangeOfString:@"WBL"];
                        if ((isRange.location!=NSNotFound||[movement isEqualToString:@"WBTL"])) {
                            greenLabel.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"BlueUpLeft.png"]];
                            //                    greenLabel.backgroundColor=[UIColor colorWithRed:176 green:226 blue:255 alpha:1.0];
                            yellowLabel.backgroundColor=[UIColor yellowColor];
                            NSLog(@"%@\n",movement);
                        }
                    }
                }
                    break;
                    
                    
                default:
                    break;
            }
        }
        [self.labelList addObject:greenLabel];
        [self.labelList addObject:yellowLabel];
        [self.labelList addObject:allRedLabel];
//        [self.labelList addObject:allRedLabel];
        UILabel *offsetLabel = [[[UILabel alloc] initWithFrame:CGRectMake(allRedLabel.frame.origin.x+allRedLabel.frame.size.width,0,1,self.subActivePhaseLabel.frame.size.height)] autorelease];
        offsetLabel.backgroundColor = [UIColor blueColor];
        [self.subActivePhaseLabel addSubview:greenLabel];
        [self.subActivePhaseLabel addSubview:yellowLabel];
        [self.subActivePhaseLabel addSubview:allRedLabel];
        [self.subActivePhaseLabel addSubview:offsetLabel];
        self.timingRing2GraphPositionX=self.timingRing2GraphPositionX+allRedPhaseLength;
    }
    {
        UILabel *barrierLabel = [[[UILabel alloc] initWithFrame:CGRectMake(barrierPositionX,timingGraphPositionY,5,timingGraphHeight)] autorelease];
        barrierLabel.backgroundColor = [UIColor blackColor];
//        [self.labelList addObject:barrierLabel];
        [self.subActivePhaseLabel addSubview:barrierLabel];
    }
    
    for (int j=0; j<self.labelsExtensionNumber; j++) {
        self.timingRing2GraphPositionX=TIMING_RING2_GRAPH_INITIAL_POSITION_X;
        for (int i=0; i<ring2PhaseList.count; i++) {
            SignalPhase *phase=[ring2PhaseList objectAtIndex:i];
            if ([phase.barrier intValue]==2 && self.timingRing2GraphPositionX<barrierPositionX) {
                self.timingRing2GraphPositionX=barrierPositionX;
            }
            int greenPhaseLength=round(320*[phase.greenTime doubleValue]/[self.timingPattern.cycleLength intValue]);
            int yellowPhaseLength=round(320*[phase.yellowTime doubleValue]/[self.timingPattern.cycleLength intValue]);
            int allRedPhaseLength=round(320*[phase.allRedTime doubleValue]/[self.timingPattern.cycleLength intValue]);
            UILabel *greenLabel = [[[UILabel alloc] initWithFrame:CGRectMake(self.timingRing2GraphPositionX+round((j+1)*320*[self.signal.cycleLength doubleValue]/[self.timingPattern.cycleLength doubleValue]),timingGraphPositionY,greenPhaseLength,timingGraphHeight)] autorelease];
            greenLabel.tag=200+i;
            //        greenLabel.text =[phase.phaseID stringByAppendingString:phase.phaseMovement];
            greenLabel.font = [UIFont boldSystemFontOfSize:8];
            greenLabel.textAlignment = UITextAlignmentCenter;
            greenLabel.backgroundColor = [UIColor redColor];
            greenLabel.textColor=[UIColor blackColor];
            //        [self addLabelResponder:greenLabel];
            //        [self.labelList addObject:greenLabel];
            //        [self.subActivePhaseLabel addSubview:greenLabel];
            
            //        [self.labelList addObject:greenLabel];
            //        UILabel * tempLabel=[self.labelList objectAtIndex:self.labelList.count-1];
            //        [[self.labelList objectAtIndex:self.labelList.count-1] removeFromSuperview];
            
            self.timingRing2GraphPositionX=self.timingRing2GraphPositionX+greenPhaseLength;
            
            UILabel *yellowLabel = [[[UILabel alloc] initWithFrame:CGRectMake(self.timingRing2GraphPositionX+round((j+1)*320*[self.signal.cycleLength doubleValue]/[self.timingPattern.cycleLength doubleValue]),timingGraphPositionY,yellowPhaseLength,timingGraphHeight)] autorelease];
            yellowLabel.backgroundColor = [UIColor redColor];
            
            //        [self.labelList addObject:yellowLabel];
            //        [self.subActivePhaseLabel addSubview:yellowLabel];
            self.timingRing2GraphPositionX=self.timingRing2GraphPositionX+yellowPhaseLength;
            
            UILabel *allRedLabel = [[[UILabel alloc] initWithFrame:CGRectMake(self.timingRing2GraphPositionX+round((j+1)*320*[self.signal.cycleLength doubleValue]/[self.timingPattern.cycleLength doubleValue]),timingGraphPositionY,allRedPhaseLength,timingGraphHeight)] autorelease];
            allRedLabel.backgroundColor = [UIColor redColor];
            
            //        [self.labelList addObject:allRedLabel];
            //        [self.subActivePhaseLabel addSubview:allRedLabel];
            {
                NSString* phaseMovement=phase.phaseMovement;
                NSMutableArray* movementList=[[[NSMutableArray alloc] init] autorelease];
                NSRange commaRange=[phaseMovement rangeOfString:@","];
                while (commaRange.location!=NSNotFound) {
                    NSString *movement=[phaseMovement substringToIndex:commaRange.location];
                    [movementList addObject:movement];
                    phaseMovement=[phaseMovement substringFromIndex:(commaRange.location+1)];
                    commaRange=[phaseMovement rangeOfString:@","];
                }
                if (phaseMovement.length>0) {
                    [movementList addObject:phaseMovement];
                }
                
                switch (self.direction) {
                    case NORTHBOUND:
                    case SOUTHBOUND:
                    {
                        for (NSString *movement in movementList) {
                            NSRange isRange=[movement rangeOfString:@"NBT"];
                            if ((isRange.location!=NSNotFound||[movement isEqualToString:@"NBSB"] ||[movement isEqualToString:@"NBTL"])) {
                                if (yellowLabel.backgroundColor!=[UIColor yellowColor]) {
                                    greenLabel.backgroundColor=[UIColor blueColor];
                                    greenLabel.textColor=[UIColor blackColor];
                                    yellowLabel.backgroundColor=[UIColor yellowColor];
                                }
                                InterfacePoint *greenTopLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                                InterfacePoint *yellowTopRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                                [self.nbandLeftEndPoints addObject:greenTopLeftPoint];
                                [self.nbandRightEndPoints addObject:yellowTopRightPoint];
                                InterfacePoint *greenBottomLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                                InterfacePoint *yellowBottomRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                                [self.nbandLeftBeginPoints addObject:greenBottomLeftPoint];
                                
                                [self.nbandrightBeginPoints addObject:yellowBottomRightPoint];
                                NSLog(@"%@\n",movement);
                            }
                            //                        [movement isEqualToString:@"NBT"]||[movement isEqualToString:@"NBSB"] ||[movement isEqualToString:@"SBTL"]||
                            isRange=[movement rangeOfString:@"NBL"];
                            if ((isRange.location!=NSNotFound||[movement isEqualToString:@"NBTL"])) {
                                greenLabel.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"BlueUpLeft.png"]];
                                //                    greenLabel.backgroundColor=[UIColor colorWithRed:176 green:226 blue:255 alpha:1.0];
                                yellowLabel.backgroundColor=[UIColor yellowColor];
                                NSLog(@"%@\n",movement);
                            }
                        }
                    }
                        
                    {
                        for (NSString *movement in movementList) {
                            NSRange isRange=[movement rangeOfString:@"SBT"];
                            if ((isRange.location!=NSNotFound||[movement isEqualToString:@"NBSB"] ||[movement isEqualToString:@"SBTL"])) {
                                if (yellowLabel.backgroundColor!=[UIColor yellowColor]) {
                                    greenLabel.backgroundColor=[UIColor greenColor];
                                    greenLabel.textColor=[UIColor blackColor];
                                    yellowLabel.backgroundColor=[UIColor yellowColor];
                                }
                                InterfacePoint *greenTopLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                                InterfacePoint *yellowTopRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                                [self.sbandLeftEndPoints addObject:greenTopLeftPoint];
                                [self.sbandRightEndPoints addObject:yellowTopRightPoint];
                                InterfacePoint *greenBottomLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                                InterfacePoint *yellowBottomRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                                [self.sbandLeftBeginPoints addObject:greenBottomLeftPoint];
                                
                                [self.sbandrightBeginPoints addObject:yellowBottomRightPoint];
                                NSLog(@"%@\n",movement);
                            }
                            //                        [movement isEqualToString:@"NBT"]||[movement isEqualToString:@"NBSB"] ||[movement isEqualToString:@"SBTL"]||
                            isRange=[movement rangeOfString:@"SBL"];
                            if ((isRange.location!=NSNotFound||[movement isEqualToString:@"SBTL"])) {
                                greenLabel.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"DownLeft.png"]];
                                //                    greenLabel.backgroundColor=[UIColor colorWithRed:176 green:226 blue:255 alpha:1.0];
                                yellowLabel.backgroundColor=[UIColor yellowColor];
                                NSLog(@"%@\n",movement);
                            }
                        }
                    }
                        break;
                    case EASTBOUND:
                    case WESTBOUND:
                    {
                        for (NSString *movement in movementList) {
                            NSRange isRange=[movement rangeOfString:@"EBT"];
                            if ((isRange.location!=NSNotFound ||[movement isEqualToString:@"EBTL"])) {
                                if (yellowLabel.backgroundColor!=[UIColor yellowColor]) {
                                    greenLabel.backgroundColor=[UIColor greenColor];
                                    greenLabel.textColor=[UIColor blackColor];
                                    yellowLabel.backgroundColor=[UIColor yellowColor];
                                }
                                InterfacePoint *greenTopLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                                InterfacePoint *yellowTopRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                                [self.ebandLeftEndPoints addObject:greenTopLeftPoint];
                                [self.ebandRightEndPoints addObject:yellowTopRightPoint];
                                InterfacePoint *greenBottomLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                                InterfacePoint *yellowBottomRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                                [self.ebandLeftBeginPoints addObject:greenBottomLeftPoint];
                                
                                [self.ebandrightBeginPoints addObject:yellowBottomRightPoint];
                                NSLog(@"%@\n",movement);
                            }
                            //                        [movement isEqualToString:@"WBT"] ||[movement isEqualToString:@"WBTL"]||
                            isRange=[movement rangeOfString:@"EBL"];
                            if ((isRange.location!=NSNotFound||[movement isEqualToString:@"EBTL"])) {
                                greenLabel.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"DownLeft.png"]];
                                //                    greenLabel.backgroundColor=[UIColor colorWithRed:176 green:226 blue:255 alpha:1.0];
                                yellowLabel.backgroundColor=[UIColor yellowColor];
                                NSLog(@"%@\n",movement);
                            }
                        }
                    }
                        
                    {
                        for (NSString *movement in movementList) {
                            NSRange isRange=[movement rangeOfString:@"WBT"];
                            if ((isRange.location!=NSNotFound ||[movement isEqualToString:@"WBTL"])) {
                                if (yellowLabel.backgroundColor!=[UIColor yellowColor]) {
                                    greenLabel.backgroundColor=[UIColor blueColor];
                                    greenLabel.textColor=[UIColor blackColor];
                                    yellowLabel.backgroundColor=[UIColor yellowColor];
                                }
                                InterfacePoint *greenTopLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                                InterfacePoint *yellowTopRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.signalNameLabel.frame.size.height] autorelease];
                                [self.wbandLeftEndPoints addObject:greenTopLeftPoint];
                                [self.wbandRightEndPoints addObject:yellowTopRightPoint];
                                InterfacePoint *greenBottomLeftPoint=[[[InterfacePoint alloc] initWithXY:greenLabel.frame.origin.x+self.frame.origin.x+self.subActivePhaseLabel.frame.origin.x Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                                InterfacePoint *yellowBottomRightPoint=[[[InterfacePoint alloc] initWithXY:greenTopLeftPoint.X+greenLabel.frame.size.width+yellowLabel.frame.size.width Y:SELF_FRAME_ORIGIN_Y+self.frame.size.height] autorelease];
                                [self.wbandLeftBeginPoints addObject:greenBottomLeftPoint];
                                
                                [self.wbandrightBeginPoints addObject:yellowBottomRightPoint];
                                NSLog(@"%@\n",movement);
                            }
                            //                        [movement isEqualToString:@"EBT"] ||[movement isEqualToString:@"EBTL"]||
                            isRange=[movement rangeOfString:@"WBL"];
                            if ((isRange.location!=NSNotFound||[movement isEqualToString:@"WBTL"])) {
                                greenLabel.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"BlueUpLeft.png"]];
                                //                    greenLabel.backgroundColor=[UIColor colorWithRed:176 green:226 blue:255 alpha:1.0];
                                yellowLabel.backgroundColor=[UIColor yellowColor];
                                NSLog(@"%@\n",movement);
                            }
                        }
                    }
                        break;
                        
                        
                    default:
                        break;
                }
            }
            [self.labelList addObject:greenLabel];
            [self.labelList addObject:yellowLabel];
            [self.labelList addObject:allRedLabel];
            //        [self.labelList addObject:allRedLabel];
            
            //        int offsetLabelPositionX=20;
            UILabel *offsetLabel = [[[UILabel alloc] initWithFrame:CGRectMake(allRedLabel.frame.origin.x+allRedLabel.frame.size.width,0,1,self.subActivePhaseLabel.frame.size.height)] autorelease];
            offsetLabel.backgroundColor = [UIColor blueColor];
            [self.subActivePhaseLabel addSubview:greenLabel];
            [self.subActivePhaseLabel addSubview:yellowLabel];
            [self.subActivePhaseLabel addSubview:allRedLabel];
            [self.subActivePhaseLabel addSubview:offsetLabel];
            self.timingRing2GraphPositionX=self.timingRing2GraphPositionX+allRedPhaseLength;
        }
        {
            UILabel *barrierLabel = [[[UILabel alloc] initWithFrame:CGRectMake(barrierPositionX+round((j+1)*320*[self.signal.cycleLength doubleValue]/[self.timingPattern.cycleLength doubleValue]),timingGraphPositionY,5,timingGraphHeight)] autorelease];
            barrierLabel.backgroundColor = [UIColor blackColor];
            //        [self.labelList addObject:barrierLabel];
            [self.subActivePhaseLabel addSubview:barrierLabel];
        }
    }
    

    
    
    
    [self.activePhaseLabel addSubview:self.subActivePhaseLabel];
//    [self.activePhaseLabel addSubview:offsetLabel];
    
    
}



- (void)updateActivePhaseLabelWithMasterTime:(int)masterTime
{
    int time=round(([self.signal.offset intValue]-masterTime-self.offsetPhaseTime)*320/[self.timingPattern.cycleLength intValue]);
//    time=time+80;
    int cycleLengPixels=round(320*[self.signal.cycleLength doubleValue]/[self.timingPattern.cycleLength doubleValue]);
    
    while (time>0) {
        time=time-cycleLengPixels;
    }
    while (time<-cycleLengPixels) {
        time=time+cycleLengPixels;
    }
    
    CGRect frame=self.subActivePhaseLabel.frame;
    for (InterfacePoint *point in self.nbandLeftBeginPoints) {
        point.X=point.X+(time-frame.origin.x);
        while (point.X<-cycleLengPixels) {
            point.X=point.X+cycleLengPixels;
        }
    }
    for (InterfacePoint *point in self.nbandrightBeginPoints) {
        point.X=point.X+(time-frame.origin.x);
        while (point.X<-cycleLengPixels) {
            point.X=point.X+cycleLengPixels;
        }
    }
    for (InterfacePoint *point in self.nbandLeftEndPoints) {
        point.X=point.X+(time-frame.origin.x);
        while (point.X<-cycleLengPixels) {
            point.X=point.X+cycleLengPixels;
        }
    }
    for (InterfacePoint *point in self.nbandRightEndPoints) {
        point.X=point.X+(time-frame.origin.x);
        while (point.X<-cycleLengPixels) {
            point.X=point.X+cycleLengPixels;
        }
    }
    
    
    for (InterfacePoint *point in self.sbandLeftBeginPoints) {
        point.X=point.X+(time-frame.origin.x);
        while (point.X<-cycleLengPixels) {
            point.X=point.X+cycleLengPixels;
        }
    }
    for (InterfacePoint *point in self.sbandrightBeginPoints) {
        point.X=point.X+(time-frame.origin.x);
        while (point.X<-cycleLengPixels) {
            point.X=point.X+cycleLengPixels;
        }
    }
    for (InterfacePoint *point in self.sbandLeftEndPoints) {
        point.X=point.X+(time-frame.origin.x);
        while (point.X<-cycleLengPixels) {
            point.X=point.X+cycleLengPixels;
        }
    }
    for (InterfacePoint *point in self.sbandRightEndPoints) {
        point.X=point.X+(time-frame.origin.x);
        while (point.X<-cycleLengPixels) {
            point.X=point.X+cycleLengPixels;
        }
    }
    
    
    for (InterfacePoint *point in self.ebandLeftBeginPoints) {
        point.X=point.X+(time-frame.origin.x);
        while (point.X<-cycleLengPixels) {
            point.X=point.X+cycleLengPixels;
        }
    }
    for (InterfacePoint *point in self.ebandrightBeginPoints) {
        point.X=point.X+(time-frame.origin.x);
        while (point.X<-cycleLengPixels) {
            point.X=point.X+cycleLengPixels;
        }
    }
    for (InterfacePoint *point in self.ebandLeftEndPoints) {
        point.X=point.X+(time-frame.origin.x);
        while (point.X<-cycleLengPixels) {
            point.X=point.X+cycleLengPixels;
        }
    }
    for (InterfacePoint *point in self.ebandRightEndPoints) {
        point.X=point.X+(time-frame.origin.x);
        while (point.X<-cycleLengPixels) {
            point.X=point.X+cycleLengPixels;
        }
    }
    
    
    for (InterfacePoint *point in self.wbandLeftBeginPoints) {
        point.X=point.X+(time-frame.origin.x);
        while (point.X<-cycleLengPixels) {
            point.X=point.X+cycleLengPixels;
        }
    }
    for (InterfacePoint *point in self.wbandrightBeginPoints) {
        point.X=point.X+(time-frame.origin.x);
        while (point.X<-cycleLengPixels) {
            point.X=point.X+cycleLengPixels;
        }
    }
    for (InterfacePoint *point in self.wbandLeftEndPoints) {
        point.X=point.X+(time-frame.origin.x);
        while (point.X<-cycleLengPixels) {
            point.X=point.X+cycleLengPixels;
        }
    }
    for (InterfacePoint *point in self.wbandRightEndPoints) {
        point.X=point.X+(time-frame.origin.x);
        while (point.X<-cycleLengPixels) {
            point.X=point.X+cycleLengPixels;
        }
    }
    frame.origin=CGPointMake(time, frame.origin.y);
    self.subActivePhaseLabel.frame=frame;
    
    
    
}

- (void)updateLabelsExtensionNumber:(int)labelsExtenstionNumber
{
    self.labelsExtensionNumber=labelsExtenstionNumber;
    
     {
         [self.signalNameLabel removeFromSuperview];
         [self.activePhaseLabel removeFromSuperview];
         self.nbandLeftBeginPoints=[[[NSMutableArray alloc] init] autorelease];
         self.nbandLeftEndPoints=[[[NSMutableArray alloc] init] autorelease];
         self.nbandrightBeginPoints=[[[NSMutableArray alloc] init] autorelease];
         self.nbandRightEndPoints=[[[NSMutableArray alloc] init] autorelease];
         self.sbandLeftBeginPoints=[[[NSMutableArray alloc] init] autorelease];
         self.sbandLeftEndPoints=[[[NSMutableArray alloc] init] autorelease];
         self.sbandrightBeginPoints=[[[NSMutableArray alloc] init] autorelease];
         self.sbandRightEndPoints=[[[NSMutableArray alloc] init] autorelease];
         self.ebandLeftBeginPoints=[[[NSMutableArray alloc] init] autorelease];
         self.ebandLeftEndPoints=[[[NSMutableArray alloc] init] autorelease];
         self.ebandrightBeginPoints=[[[NSMutableArray alloc] init] autorelease];
         self.ebandRightEndPoints=[[[NSMutableArray alloc] init] autorelease];
         self.wbandLeftBeginPoints=[[[NSMutableArray alloc] init] autorelease];
         self.wbandLeftEndPoints=[[[NSMutableArray alloc] init] autorelease];
         self.wbandrightBeginPoints=[[[NSMutableArray alloc] init] autorelease];
         self.wbandRightEndPoints=[[[NSMutableArray alloc] init] autorelease];
         self.signalNameLabel=[[[UILabel alloc] initWithFrame:CGRectMake(15, 2, 300, 10)] autorelease];
         self.signalNameLabel.backgroundColor=[UIColor whiteColor];
         self.signalNameLabel.numberOfLines=0;
         self.signalNameLabel.lineBreakMode=UILineBreakModeWordWrap;
         UIFont *cellFont = [ UIFont fontWithName: @"Arial" size: 10.0 ];
         self.signalNameLabel.minimumFontSize=5;
         //        self.signalNameLabel.font  = cellFont;
         //        self.signalNameLabel.layer.cornerRadius=8;
         self.signalNameLabel.text=self.signal.signalName;
         self.signalNameLabel.font=cellFont;
         self.signalNameLabel.textAlignment=UITextAlignmentRight;
         
         self.activePhaseLabel=[[[UILabel alloc] initWithFrame:CGRectMake(0, 11, 320*(self.labelsExtensionNumber+1), 10)] autorelease];
         self.activePhaseLabel.backgroundColor=[UIColor clearColor];
         self.activePhaseLabel.numberOfLines=0;
         self.activePhaseLabel.lineBreakMode=UILineBreakModeWordWrap;
         self.activePhaseLabel.minimumFontSize=5;
         self.activePhaseLabel.font  = cellFont;
         self.activePhaseLabel.layer.cornerRadius=0;
         self.activePhaseLabel.layer.borderColor = [UIColor blackColor].CGColor;
         self.activePhaseLabel.layer.borderWidth = 1.0;
         self.labelList=[[[NSMutableArray alloc] init] autorelease];
          [self drawSignalGraph];
         [self addSubview:self.signalNameLabel];
         int signalNameNumber=self.activePhaseLabel.frame.size.width/320;
         for (int i=1; i<signalNameNumber; i++) {
             UILabel *tempSignalNameLabel=[[[UILabel alloc] initWithFrame:CGRectMake(15+320*i, 2, 300, 10)] autorelease];
             tempSignalNameLabel.backgroundColor=[UIColor whiteColor];
             tempSignalNameLabel.numberOfLines=0;
             tempSignalNameLabel.lineBreakMode=UILineBreakModeWordWrap;
             
             tempSignalNameLabel.minimumFontSize=5;
             //        self.signalNameLabel.font  = cellFont;
             //        self.signalNameLabel.layer.cornerRadius=8;
             tempSignalNameLabel.text=self.signal.signalName;
             tempSignalNameLabel.font=cellFont;
             tempSignalNameLabel.textAlignment=UITextAlignmentRight;
             [self addSubview:tempSignalNameLabel];
         }
         [self addSubview:self.activePhaseLabel];
     }
    self.frame=CGRectMake(self.frame.origin.x,self.frame.origin.y, 320*(self.labelsExtensionNumber+1), self.frame.size.height);
    
    
    [self updateActivePhaseLabelWithMasterTime:0];
//    for (int k=0; k<self.nbandLeftBeginPoints.count; k++) {
//        InterfacePoint *beginPoint=[self.nbandLeftBeginPoints objectAtIndex:k];
//        InterfacePoint *endPoint=[self.nbandLeftEndPoints objectAtIndex:k];
//        NSLog(@"\n index: %d, beginPoint: %f,%f, endPoint: %f,%f", k, beginPoint.X,beginPoint.Y,endPoint.X,endPoint.Y);
//    }
    


    
}


@end
